﻿using System;
using System.ComponentModel;
using System.Globalization;

namespace Gmantis.Controls
{
    internal class ComboBoxItemConverterBridge : StringTypeConverter
    {
        // Fields
        private ComboBox _combo;

        // Methods
        internal ComboBoxItemConverterBridge(ComboBox combo)
        {
            this._combo = combo;
        }

        public override object ConvertFromString(ITypeDescriptorContext context, CultureInfo culture, string text)
        {
            Func<string, bool> doComparission = null;
            if (text.Length == 0)
            {
                return null;
            }
            object content = null;
            if (content == null)
            {
                object selectedItem = null;
                if ((this._combo.SelectedItem != null) && (string.Compare(base.ConvertToString(this._combo.SelectedItem), text, StringComparison.Ordinal) == 0))
                {
                    selectedItem = this._combo.SelectedItem;
                }
                else
                {
                    int num = -1;
                    if (doComparission == null)
                    {
                        doComparission = x => string.Compare(x, text, StringComparison.OrdinalIgnoreCase) == 0;
                    }
                    num = this._combo.DoCircularSearch(doComparission);
                    if (num != -1)
                    {
                        selectedItem = this._combo.Items[num];
                    }
                }
                ComboBoxItem item = selectedItem as ComboBoxItem;
                if (item != null)
                {
                    content = item.Content;
                }
                else
                {
                    content = selectedItem;
                }
            }
            if ((content == null) && (this.Converter != null))
            {
                content = this.Converter.ConvertFrom(context, culture, text);
            }
            if ((content != null) || (!this._combo.IsEditable && !this._combo._isSettingText))
            {
                return content;
            }
            return text;
        }

        public object ConvertFromStringToItem(string text)
        {
            object obj2 = null;
            int num = -1;
            num = this._combo.DoCircularSearch(x => string.Compare(x, text, StringComparison.OrdinalIgnoreCase) == 0);
            if (num != -1)
            {
                obj2 = this._combo.Items[num];
            }
            if ((obj2 == null) && (this.Converter != null))
            {
                obj2 = this.Converter.ConvertFrom(text);
            }
            if ((obj2 != null) || (!this._combo.IsEditable && !this._combo._isSettingText))
            {
                return obj2;
            }
            return text;
        }

        public override string ConvertToString(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            if (value != null)
            {
                ComboBoxItem item = value as ComboBoxItem;
                if (item != null)
                {
                    value = item.Content;
                }
                if (!string.IsNullOrEmpty(this._combo.DisplayMemberPath))
                {
                    object propertyValue = value.GetPropertyValue<object>(this._combo.DisplayMemberPath);
                    if (propertyValue == null)
                    {
                        return "";
                    }
                    return (propertyValue.ToString() ?? "");
                }
                if (this.Converter != null)
                {
                    try
                    {
                        return (string)this.Converter.ConvertTo(context, culture, value, typeof(string));
                    }
                    catch (NotImplementedException)
                    {
                    }
                }
                if (value != null)
                {
                    return value.ToString();
                }
            }
            return "";
        }

        // Properties
        public TypeConverter Converter { get; set; }
    }
}
